<?php
namespace App\Api\Controllers;

use Illuminate\Http\Request;
use Dingo\Api\Routing\Helpers;

use App\Http\Controllers\Controller as BaseController;

class Controller extends BaseController
{
    use Helpers;

    /**
     * 正常返回
     * @param array $data
     * @return mixed
     */
    public function success($data=[],$status_code=200){
        return $this->response->array(method_exists($data,'toArray')?$data->toArray():$data)->setStatusCode($status_code);
    }

    /**
     * 异常返回
     * @param string $message
     * @param int $status_code
     * @return mixed
     */
    public function error($message='操作成功',$status_code=500){
        return $this->response->array(['code'=>$status_code,'message'=>$message])->setStatusCode($status_code);
    }
    
    public function error1($message='操作成功',$status_code=200){
        return $this->response->array(['code'=>$status_code,'message'=>$message])->setStatusCode($status_code);
    }

    public function errorResponse($statusCode, $message=null, $code=0)
    {
        throw new HttpException($statusCode, $message, null, [], $code);
    }
}